package com.atguigu.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5;
import com.atguigu.util.ValidateCodeUtils;
import com.atguigu.vo.LoginVo;
import com.atguigu.vo.RegisterVo;
import com.sun.org.apache.bcel.internal.classfile.PMGClass;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lhh
 * @ClassName UserInfoController
 * @date 2022/9/23 20:54
 */

@RestController
@RequestMapping("userInfo")
public class UserInfoController {

    @Reference
    private UserInfoService userInfoService;

    @PostMapping("register")
    private Result register(@RequestBody RegisterVo registerVo, HttpServletRequest request) {

        //对前台传入的数据进行校验
        String nickName = registerVo.getNickName();
        String password = registerVo.getPassword();
        String phone = registerVo.getPhone();
        String code = registerVo.getCode();
        if (StringUtils.isEmpty(nickName) || StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //检验验证码是否输入正确
        String attCode = (String) request.getSession().getAttribute("CODE");
        if (!code.equals(attCode)) {
            return Result.build(null, ResultCodeEnum.CODE_ERROR);
        }
        //查询电话号是否已存在
        UserInfo userInfo = userInfoService.findPhone(phone);
        if (userInfo != null) {
            return Result.build(null, ResultCodeEnum.PHONE_REGISTER_ERROR);
        }
        UserInfo info = new UserInfo();
        info.setNickName(nickName);
        info.setPassword(MD5.encrypt(password));
        info.setPhone(phone);
        info.setStatus(1);
        try {
            userInfoService.save(info);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.build(null, ResultCodeEnum.FAIL);
        }
        return Result.ok();
    }

    @GetMapping("sendCode/{phone}")
    public Result sendCode(@PathVariable String phone,HttpServletRequest request){
        String code= ValidateCodeUtils.generateValidateCode4String(6);
        request.getSession().setAttribute("CODE",code);
        return Result.ok(code);
    }

    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo,HttpServletRequest request){
        String phone = loginVo.getPhone();
        String password = loginVo.getPassword();
        //非空校验
        if (StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(phone)) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        UserInfo userInfo = userInfoService.findPhone(phone);
        //账号是否正确
        if(userInfo==null){
            return Result.build(null,ResultCodeEnum.ACCOUNT_ERROR);
        }
        //密码是否正确
        if(!MD5.encrypt(password).equals(userInfo.getPassword())){
            return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
        }
        //账号是否锁定
        if(userInfo.getStatus()!=1){
            return Result.build(null,ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
        request.getSession().setAttribute("USER",userInfo);
        Map map = new HashMap();
        map.put("nickName", userInfo.getNickName());
        map.put("phone",userInfo.getPhone());
        return Result.ok(map);
    }

    @GetMapping("logout")
    public Result logout(HttpServletRequest request){
        request.getSession().invalidate();
        return Result.ok();
    }

}

